package com.ddwl.user.controller;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.user.IdentityCardQueryBo;
import com.ddwl.schema.bo.user.IdentityCardRejectBo;
import com.ddwl.schema.bo.user.IdentitySubmitBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.user.IdentityCardVo;
import com.ddwl.user.service.IdentityCardServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@Api(tags = "身份认证相关接口")
@RequestMapping("/identity")
public class IdentityCardController extends BaseController {

    @Resource
    private IdentityCardServer identityCardServer;

    @PostMapping
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.FENXIAO})
    @ApiOperation("提交身份认证申请,可重复提交")
    public RestVo create(@Valid @RequestBody IdentitySubmitBo bo) {
        bo.setUserId(getUserId());
        identityCardServer.submitProfitApply(bo);
        return RestVo.ok();
    }

    @GetMapping("{applyId}")
    @ApiOperation("获取审批详情")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo<IdentityCardVo> getApply(@PathVariable("applyId") String applyId) {
        return RestVo.ok(identityCardServer.get(applyId));
    }


    @DeleteMapping("{applyId}")
    @ApiOperation("解绑用户身份认证信息")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo<IdentityCardVo> delete(@PathVariable("applyId") String applyId) {
        identityCardServer.delete(applyId);
        return RestVo.ok();
    }

    @GetMapping("/info")
    @ApiOperation("获取用户申请审批详情")
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.FENXIAO})
    public RestVo<IdentityCardVo> getUserApply() {
        return RestVo.ok(identityCardServer.getUserApply(getUserId()));
    }

    @PostMapping("/page")
    @ApiOperation("获取审批列表分页")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo<PageVo<IdentityCardVo>> getApply(@RequestBody PageBo<IdentityCardQueryBo> pageBo) {
        return RestVo.ok(identityCardServer.page(pageBo));
    }


    @PostMapping("/{applyId}/approve")
    @ApiOperation("审批通过")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo approveApply(@PathVariable("applyId") String applyId) {
        identityCardServer.approveApply(applyId);
        return RestVo.ok();
    }

    @PostMapping("/reject")
    @ApiOperation("审批拒绝")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo rejectApply(@RequestBody IdentityCardRejectBo identityCardRejectBo) {
        identityCardServer.rejectApply(identityCardRejectBo);
        return RestVo.ok();
    }

    @GetMapping("/successful")
    @ApiOperation("查询审批通过的")
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<List<IdentityCardVo>> successfulApply() {
        return RestVo.ok(identityCardServer.querySuccessfulApply());
    }

    @GetMapping("/getIdCardByCodeAndUserId/{code}/{userId}")
    @ApiOperation("根据卡号用户id证查询")
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<List<IdentityCardVo>> getIdCardByCodeAndUserId(@PathVariable("code") String code, @PathVariable("userId") String userId) {
        return RestVo.ok(identityCardServer.getIdCardByCodeAndUserId(code, userId));
    }

    @GetMapping("/getIdCardByCode/{code}")
    @ApiOperation("根据卡号查询")
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<List<IdentityCardVo>> getIdCardByCode(@PathVariable("code") String code) {
        return RestVo.ok(identityCardServer.getIdCardByCode(code));
    }

    @ApiOperation("根据用户id查询")
    @Auth(product = BaseEnum.ProductEnum.API)
    @GetMapping("/getIdCardByUserId/{userId}")
    public RestVo<IdentityCardVo> getIdCardByUserId(@PathVariable("userId") String userId) {
        return RestVo.ok(identityCardServer.getIdCardByUserId(userId));
    }

    @ApiOperation("查询用户是否实名")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM})
    @GetMapping("/getIdCardAuth")
    public RestVo<Boolean> getIdCardAuth() {
        return RestVo.ok(identityCardServer.getIdCardByUserId(getUserId()) != null);
    }

    @ApiOperation("根据用户电话查询")
    @Auth(product = BaseEnum.ProductEnum.API)
    @GetMapping("/getIdCardByTel/{tel}")
    public RestVo<IdentityCardVo> getIdCardByTel(@PathVariable("tel") String tel) {
        return RestVo.ok(identityCardServer.getIdCardByTel(tel));
    }

    @PostMapping("/changeId")
    @ApiOperation("把id反转成时间戳")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo changeId() {
        identityCardServer.changeId();
        return RestVo.ok();
    }

}
